var VProxy;
var thisObj;
var ReportsPageMediator = Objs("view.ReportsPageMediator",
	Mediator,
{

	initialize: function( name, viewComponent )
	{ 
		
		ReportsPageMediator.$super.initialize.call( this, name, viewComponent );
    	VProxy = this.facade.retrieveProxy( ProxyNames.VEHICLE_PROXY );
		this.registerListeners();
		thisObj = this;
		
		
	},
	registerListeners: function()
	{   
		var reportsPage = this.get();
		reportsPage.addEventListener( ReportsPage.LOAD_REPORTS, this.loadReports, this );
		
	},
	get: function()
	{
		return this.viewComponent;
	},
	unregisterListeners: function()
	{
	},
	listNotificationInterests: function()
	{
		return [
				NotificationNames.REPORT
		];
	},
    handleNotification: function( note )
	{ 
		switch ( note.getName() )
		{ 
			case NotificationNames.REPORT:
		        this.setReports(note.getBody());
				break;
		}
		

	},
	loadReports: function()
	{ 
		
		if($("#inpFromDate").val() == "" || $("#inpToDate").val()=="")
		{
			alert("Fill From And To Date Fields");
			return null;
		}

		var from=$("#inpFromDate").val();
		var x=from.split("/");
		var fdate=new Date(x[0],(x[1]-1),x[2]);
		from=x[2]+"-"+x[0]+"-"+x[1];
		var to=$("#inpToDate").val();
		var y=to.split("/");
		var tdate=new Date(y[0],(y[1]-1),y[2]);
		to=y[2]+"-"+y[0]+"-"+y[1];
		var diff=tdate-fdate;
		
		var today = new Date();
		var dd = today.getDate();
		var mm = today.getMonth();
		var yyyy = today.getFullYear();

		if(fdate > new Date(yyyy,mm,dd ))
		{
			
		}
		if(diff<=0)
		{
			alert("Enter Correct dates");
		}
		else
		{
			VProxy.getreports(from, to);
		}
		//VProxy.getLockedVehicles();
	},
	setReports:function(result)
	{
	
	//table_reports
	
		var tbodyInnerHtml = "<tbody>";
		var reports = result.reportarray;

		$('#table_reports tbody').html(" ");
		for(var i=0; i<reports.length; i++)
		{
			var row = reports[i];
			tbodyInnerHtml += "<tr><td>"+row.date+"</td><td>"+row.via_w_seller+"</td><td>"+row.via_m_seller+"</td><td>"+row.via_w_buyer+"</td><td>"+row.via_m_buyer+"</td></tr>";
			
		}
		tbodyInnerHtml += "</tbody>";
		var table = $('#table_reports');
		table.append(tbodyInnerHtml);
		
		$('#lblSellerViaWeb').html( " " );
		$('#lblSellerViaMob').html( " " );
		$('#lblBuyerViaWeb').html( " " );
		$('#lblBuyerViaMob').html( " " );
		
		var reportSummary = result.total;
		$('#lblSellerViaWeb').html(reportSummary.total_w_seller);
		$('#lblSellerViaMob').html(reportSummary.total_m_seller);
		$('#lblBuyerViaWeb').html(reportSummary.total_w_buyer);
		$('#lblBuyerViaMob').html(reportSummary.total_m_buyer);
	},
	unlockVehicle:function(button)
	{
		this.VProxy.unlockVehicle(button.id);
	}
	
	
});

